package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import util.ResultSetMapper;
import util.jdbcmysql;
import vo.Matcher;


public class MatcherResultDao {

	private Connection con = null; 
	private Statement stat = null; 
	private ResultSet rs = null; 
	private PreparedStatement pst = null; 	
	
	private String tableName = "matcher_result";
	
	public MatcherResultDao()
	{
		con = new jdbcmysql().getConnection();
	}
	
	public List<Matcher> searchMatcherResult(String memberID)
	{
		ResultSetMapper<Matcher> resultSetMapper = new ResultSetMapper<Matcher>();
		List<Matcher> pojoList = new ArrayList<Matcher>();
		
		String sqlStr = String.format("select * from "+tableName+" as mr where mr.member_ID = '%s' and mr.result= %d "
				, memberID,0); //matcher_result => 0:���ݰt��B1:�t�令�\
	    try 
	    { 
	      stat = con.createStatement(); 
	      rs = stat.executeQuery(sqlStr); 
	      System.out.println("selectSQL:"+sqlStr);	
	      
	      pojoList = resultSetMapper.mapRersultSetToObject(rs, Matcher.class);
	      
	    } 
	    catch(SQLException e) 
	    { 
	      System.out.println("searchMatcherResult DropDB Exception :" + e.toString()); 
	    } 
	    finally 
	    { 
	      Close(); 
	    }
				
		return pojoList;
	}
	
	public boolean updateMatcherResultByID(String memberID,String matcherID)
	{
		boolean res = false;
		String sqlStr = String.format("UPDATE "+tableName+" as mr SET mr.result =1 where (mr.member_ID = '%s' And mr.matcher_member_ID = '%s' )"
				, memberID,matcherID); 
	    try 
	    { 
	      stat = con.createStatement(); 
	      int check = stat.executeUpdate(sqlStr); 
	      System.out.println("selectSQL:"+sqlStr);	
	      
	      if(check !=0)
	      {
	    	  res = true;
	      }
	      
	    } 
	    catch(SQLException e) 
	    { 
	      System.out.println("updateMatcherResultByID DropDB Exception :" + e.toString()); 
	    } 
	    finally 
	    { 
	      Close(); 
	    }		
		
		return res;
	}
	
	public boolean insertMatcherResult(String memberID,String matcherID,String memberName,String matcherName)
	{
		boolean res = false;
		String sqlStr = String.format("INSERT INTO "+tableName+" (member_ID,matcher_member_ID,result,tunnel_ID,member_name,matcher_member_name) VALUES ('%s','%s','%d','%s','%s','%s')",
				matcherID,memberID,0,null,matcherName,memberName);
		
	    try 
	    { 
	      stat = con.createStatement(); 
	      int check = stat.executeUpdate(sqlStr); 
	      System.out.println("selectSQL:"+sqlStr);	
	      
	      if(check !=0)
	      {
	    	  res = true;
	      }
	      
	    } 
	    catch(SQLException e) 
	    { 
	      System.out.println("insertMatcherResult DropDB Exception :" + e.toString()); 
	    } 
	    finally 
	    { 
	      Close(); 
	    }		
		
		return res;
		
	}
	
	
	private void Close() 
	{ 
	  try 
	  { 
	    if(rs!=null) 
	    { 
	      rs.close(); 
	      rs = null; 
	    } 
	    if(stat!=null) 
	    { 
	      stat.close(); 
	      stat = null; 
	    } 
	    if(pst!=null) 
	    { 
	      pst.close(); 
	      pst = null; 
	    } 
	  } 
	 catch(SQLException e) 
	  { 
	    System.out.println("Close Exception :" + e.toString()); 
	  } 
	}	
	
}
